home *** CD-ROM | disk | FTP | other *** search
- #!/usr/local/bin/perl
- #
- # Program to read in the named.hosts file, and generate
- # coresponding reverse-lookup files (.inaddr.arpa), one per subnet.
- #
- # Eric Murray 5/23/93
- # ericm@microunity.com
-
-
- $hosts = "named.hosts";
-
- # list of Class C domains (without the class C part)
- # that we want to generate reverse maps for
-
- @magic = ( "192.216",);
-
- # what's our domain, and who's responsible for this mess?
- $domain = "microunity.com.";
- $master = "ericm.angst";
-
-
- open(FH,$hosts) || die "can't open $hosts";
- while (<FH>) {
- chop;
- s/;.*$//;
-
- next if /^$/;
-
- # first, look for the SOA so we can get it for the .rev files..
- if (/SOA/ ) { $in_soa++; next;}
-
-
- if ($in_soa) {
- s/\@//; # strip the @
- if ($_ =~ /\)/) {
- # end of SOA
- $in_soa = 0;
- $soa .= "\n" . $_ . "\n";
- next;
- }
- else {
- $soa .= "\n" . $_;
- next;
- }
- }
-
- # if we're here, we have the SOA.
-
- local($subnet);
- @a = split;
- if ($a[1] =~ /^A$/ ) {
- # an address
- # but skip localhost:
- next if /^localhost/;
- if (&inmagic($a[2])) {
- @addr = split(/\./,$a[2]);
- $num = $addr[2];
- $name = $a[0];
- # class C!
- $subnet = "$addr[0].$addr[1].$addr[2]";
- if (! $subnets{$subnet}) {
- # new one.
- $subnets{$subnet}++;
- #$subnet = $num;
- }
- #$address{$name} = $addr[3];
- #$names{$subnet} .= $name . " ";
-
- $names{$subnet . "." . $addr[3]} = $name;
- $numbers{$subnet} .= $addr[3] . " ";
- }
- }
- }
-
- foreach $subnet (keys %subnets) {
- if ($subnet) {
- $file = "named.rev.$subnet";
- open(FH,">$file") || die "can't open $file!\n";
- printf(FH ";\n;\n; MACHINE GENERATED FILE!\n");
- printf(FH "; DO NOT EDIT!\n;\n\n");
- printf(FH "%s.in-addr.arpa.\tIN\tSOA\t%s\t%s (", &rev($subnet),
- $domain,$master . "." . $domain);
- printf(FH "%s",$soa);
- printf(FH "\n\n");
- @subnetnums = sort(ByNum split(/ /,$numbers{$subnet}));
- foreach $num (@subnetnums){
- printf (FH "%d\t\tIN\tPTR\t%s.%s\n",$num,$names{$subnet . "." . $num},$domain);
- printf (FH "\n");
- }
- }
- close(FH);
- }
-
- sub ByNum {
- return $a - $b;
- }
-
- sub inmagic {
- local($add) = @_;
-
- foreach (@magic) {
- return 1 if ($add =~ /^$_/);
- }
- return 0;
- }
-
-
- sub rev {
- local($in) = @_;
- local($ret);
- # reverse an address:
-
- local(@a) = split('\.',$in);
- foreach (@a) {
- $ret = "$_.$ret";
- }
- $ret =~ s/\.$//;
- return($ret);
- }
-